package com.yxq.util;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * jdbc 工具类, 提供了获得连接与关闭连接的方法.
 * 
 * @author Subin
 */
public class DBUtil
{
	private static Properties props = new Properties();

	static
	{
		InputStream ips = null;

		ips = DBUtil.class.getClassLoader().getResourceAsStream("config.properties");

		try
		{
			props.load(ips);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws Exception
	{
		Connection conn = null;

		try
		{
			Class.forName(props.getProperty("drivername"));
			conn = DriverManager.getConnection(
					props.getProperty("url"), 
					props.getProperty("username"), 
					props.getProperty("password"));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw e;
		}

		return conn;
	}

	public static void close(Connection conn)
	{
		if (conn != null)
		{
			try
			{
				conn.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws Exception
	{
		System.out.println(getConnection());
		System.out.println(new BigDecimal("2.0").multiply(new BigDecimal("1.9")));
	}
}
